package com.wuseguang.springcloud.controller;


import com.wuseguang.springcloud.Filter.RequestParameterWrapper;
import com.wuseguang.springcloud.dao.repository.UserSubscriptionTemplateRepository;
import com.wuseguang.springcloud.service.UserSubscriptionTemplateService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 用户订阅的模板信息表 前端控制器
 * </p>
 *
 * @author
 * @since 2020-04-01
 */
@RestController
@RequestMapping("/jurisdiction")
public class UserSubscriptionTemplateController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private UserSubscriptionTemplateService userSubscriptionTemplateServiceImpl;


    /**
     * 查询家长端的未订阅的模板信息
     *
     * @param request
     * @param type    0校园版,1家长版
     * @return
     */
    @RequestMapping("/findUserTemplateClick")
    public Result findUserTemplateClick(RequestParameterWrapper request,
                                        @RequestParam("type") Integer type) {

        logger.info("查询家长为注册的模板id");

        //用户id
        String userId = null;

        //判断是否是家长
        if (type == 1) {
            userId = request.getParam("parentId");
        } else {
            userId = request.getParam("teacherId");
        }

        return userSubscriptionTemplateServiceImpl.findUserTemplateClick(userId, type);
    }

    /**
     * 家长教师模板订阅
     *
     * @param request
     * @param id
     * @return
     */
    @RequestMapping("/addUserTemplate")
    public Result addUserTemplate(RequestParameterWrapper request,
                                  @RequestParam("id") Integer id[],
                                  @RequestParam("type") Integer type) {
        logger.info("家长教师模板订阅");

        //用户id
        String userId = null;
        //判断是否是家长
        if (type == 1) {
            userId = request.getParam("parentId");
        } else {
            userId = request.getParam("teacherId");
        }
        return userSubscriptionTemplateServiceImpl.addUserTemplate(userId, id);
    }

    /**
     * 家长教师模板取消订阅
     *
     * @param request
     * @param templateId 腾讯模板id
     * @param type
     * @return
     */
    @RequestMapping("/deleteUserTemplate")
    public Result deleteUserTemplate(RequestParameterWrapper request,
                                     @RequestParam("templateId") String templateId[],
                                     @RequestParam("type") Integer type) {
        logger.info("家长教师模板取消订阅");

        //用户id
        String userId = null;
        //判断是否是家长
        if (type == 1) {
            userId = request.getParam("parentId");
        } else {
            userId = request.getParam("teacherId");
        }
        return userSubscriptionTemplateServiceImpl.deleteUserTemplate(userId, templateId);
    }

}

